﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PermissionDesign.Model;
using PermissionDesign.Model.Entity;
using PermissionDesign.Model.Permission;
using PermissionDesign.Model.Request;
using PermissionDesign.Model.Response;
using PermissionDesign.Service;
using PermissionDesign.Tool;

namespace PermissionDesign.Web.Controllers
{
    public class RoleController : Controller
    {
        #region 构造函数
        private RoleService _roleService;
        private PageInfoList _pageInfoList;
        private ActionInfoList _actionInfoList;
        public RoleController(RoleService roleService, PageInfoList pageInfoList, ActionInfoList actionInfoList)
        {
            _roleService = roleService;
            _pageInfoList = pageInfoList;
            _actionInfoList = actionInfoList;
        }
        #endregion

        public IActionResult Index()
        {
            //角色ID
            int roleId = int.Parse(CookieHelper.SingleTon().GetCookieValue(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingRoleIdKey), Request));
            //当前角色的拥有的功能编码
            ViewData["AuthorizeActions"] = _roleService.GetActionByRoleId(roleId);
            return View();
        }

        #region 获取角色分页列表
        /// <summary>
        /// 获取角色分页列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="search_des"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public JsonResult GetPageRoleList(int pageSize, int pageIndex, string search_des, DateTime? startTime, DateTime? endTime)
        {
            var total = 0;
            var where = new List<Expression<Func<Role, bool>>>();

            if (!string.IsNullOrEmpty(search_des))
            {
                where.Add(c => c.RoleName.Contains(search_des));
            }

            if (startTime != null)
            {
                where.Add(c => c.AddDate >= startTime);
            }

            if (endTime != null)
            {
                where.Add(c => c.AddDate <= endTime);
            }

            var content = _roleService.RolePageQuery(where, out total, pageIndex, pageSize);
            if (content.code == 200)
            {
                return Json(new { total = total, rows = content.data as List<Response_Role> });
            }
            else
            {
                return Json(new { total = 0, rows = "" });
            }
        }
        #endregion

        #region 判断角色是否存在
        /// <summary>
        /// 根据角色名称和角色ID判断角色是否存在
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult IsRoleNameExist(string roleName, int id = 0)
        {
            return Json(_roleService.IsRoleExist(c => c.RoleName == roleName && c.Id != id));
        }


        /// <summary>
        /// 根据角色编码判断角色是否存在
        /// </summary>
        /// <returns></returns>
        public JsonResult IsRoleEncodingExist(string roleEncoding)
        {
            return Json(_roleService.IsRoleExist(c => c.RoleEncoding == roleEncoding));
        }
        #endregion

        #region 添加角色
        [HttpPost]
        public ActionResult AddRole(Request_AddRole role)
        {
            ResponseModel response = _roleService.AddRole(role);
            if (response.code != 200)
            {
                Response.Redirect(@"/Wrong/Index?wrongContent=" + response.result + "&returnUrl=/Role/Index");
                return null;
            }
            else
            {
                return View("Index");
            }
        }
        #endregion

        #region 编辑角色
        [HttpPost]
        public ActionResult EditRole(Request_EditRole editRole)
        {
            ResponseModel response = _roleService.EditRole(editRole);
            if (response.code != 200)
            {
                Response.Redirect(@"/Wrong/Index?wrongContent=" + response.result + "&returnUrl=/Role/Index");
                return null;
            }
            else
            {
                return View("Index");
            }

        }
        #endregion

        #region 获取当前角色所拥有的功能权限集合
        /// <summary>
        /// 获取当前角色所拥有的功能权限集合
        /// </summary>
        /// <param name="">角色ID</param>
        /// <returns></returns>
        public string GetActionByRoleId(int roleId)
        {
            if (!_roleService.IsRoleExist(c => c.Id == roleId))
            {
                return "";
            }

            return _roleService.GetActionByRoleId(roleId);
        }
        #endregion

        #region 获取所有页面配置和功能配置
        /// <summary>
        /// 获取所有页面配置和功能配置
        /// </summary>
        /// <returns></returns>
        public JsonResult GetPageAndActionConfit()
        {
            return Json(new { pageInfoList = _pageInfoList.pageInfoList, actionInfoList = _actionInfoList.actionInfoList });
        }
        #endregion

        #region 为角色配置权限
        /// <summary>
        /// 为角色配置权限
        /// </summary>
        [HttpPost]
        public JsonResult Authorize(Request_SetRoleAuthorize authorize)
        {
            if (authorize == null)
            {
                return Json(new ResponseModel { code = 0, result = "参数有误" });
            }

            if (!_roleService.IsRoleExist(c => c.Id == authorize.roleId))
            {
                return Json(new ResponseModel { code = 0, result = "参数有误" });
            }
            else
            {
                return Json(_roleService.Authorize(authorize.roleId, authorize.actionEncodings));
            }
        }
        #endregion


    }
}